#include <iostream>
#include "MessageExtractor.h"
#include "quickfix/Message.h"
#include "../time.h"
#include "../date.h"

#include "../holder.h"

using namespace std;
using namespace stock_trade;

extern stock_trade::Holder holder;

void ExtractorUAPMessage(const FIX::Message& message)
{
    FIX::PosMaintRptID posMaintRptID;
    message.getField(posMaintRptID);

    FIX::PosReqID posReqID;
    message.getField(posReqID);

    FIX::PosReqType posReqType;
    message.getField(posReqType);
    //cout << "YYYY:" << posReqType << ":" << endl;

    FIX::TotalNumPosReports totalNumPosReports;
    message.getField(totalNumPosReports);

    FIX::PosReqResult posReqResult;
    message.getField(posReqResult);

    FIX::LastRptRequested lastRptRequested;
    message.getField(lastRptRequested);

    FIX::Text text;
    if (posReqResult != 0 && message.isSetField(text))
    {
        message.getField(text);
    }

    if (posReqType == 9)
        ExtractorUAPMoneyMessage(message);
    else if (posReqType == 0)
        ExtractorUAPStockMessage(message);
    else
        cerr << "error" << endl;
    
    if (lastRptRequested)
    {
        holder.isComplete = true;
    }
}

void ExtractorUAPMoneyMessage(const FIX::Message& message)
{
    float cash;
    //posReqType == 9, has only one group.
    //posReqType == 0, has two groups.
    //
    //SB股票余额 就是你一支股票的股数啊 
    //
    //SAV可用股票 就是你当前可以卖的股数（股票余额 -你当天购买的股以及你挂单的股）
    FIX::NoPosAmt noPosAmt;
    message.getField(noPosAmt);
    FIX::Group group(FIX::FIELD::NoPosAmt, FIX::FIELD::PosAmtType, 
                   FIX::message_order(FIX::FIELD::PosAmtType,FIX::FIELD::PosAmt, 0));
    for (int i = 1; i <= noPosAmt; i++)
    {
        message.getGroup(i, group);

        FIX::PosAmtType posAmtType;
        group.getField(posAmtType);

        FIX::PosAmt posAmt;
        group.getField(posAmt);
        
        cout << "---" << posAmtType<< "::" << posAmt <<endl;
        if (posAmtType == "FAV")
            cash = posAmt;
    }

    holder.cash = cash;
}

void ExtractorUAPStockMessage(const FIX::Message& message)
{
    float holdPrice;
    //posReqType == 9, has only one group.
    //posReqType == 0, has two groups.
    //
    //SB股票余额 就是你一支股票的股数啊 
    //
    //SAV可用股票 就是你当前可以卖的股数（股票余额 -你当天购买的股以及你挂单的股）
    FIX::NoPosAmt noPosAmt;
    message.getField(noPosAmt);
    FIX::Group groupPrice(FIX::FIELD::NoPosAmt, FIX::FIELD::PosAmtType, 
                   FIX::message_order(FIX::FIELD::PosAmtType,FIX::FIELD::PosAmt, 0));
    for (int i = 1; i <= noPosAmt; i++)
    {
        message.getGroup(i, groupPrice);

        FIX::PosAmtType posAmtType;
        groupPrice.getField(posAmtType);

        FIX::PosAmt posAmt;
        groupPrice.getField(posAmt);
        
        cout << "---" << posAmtType<< "::" << posAmt <<endl;
        if (posAmtType == "PC")
            holdPrice = posAmt;
    }
    
    FIX::Symbol symbol;
    FIX::SecurityExchange securityExchange;
    message.getField(symbol);
    message.getField(securityExchange);
    string sh_sz = "";
    if (securityExchange == "XSHG")
        sh_sz = "sh";
    else if (securityExchange == "XSHE")
        sh_sz = "sz";
    else
        sh_sz = "";
    string stockCode = sh_sz + symbol.getValue();
    string code = "";

    FIX::NoPositions noPositions;
    message.getField(noPositions);
    FIX::Group groupNumber(FIX::FIELD::NoPositions, FIX::FIELD::PosType, 
                   FIX::message_order(FIX::FIELD::PosType,FIX::FIELD::LongQty,FIX::FIELD::ShortQty, 0));
    for (int i = 1; i <= noPositions; i++)
    {
        message.getGroup(i, groupNumber);

        FIX::PosType posType;
        groupNumber.getField(posType);
        if (posType == "LB")	//昨日余额，没什么用
            continue;
        int number = 0;
        if (posType == "SB" || posType == "SAV" || posType == "SQ" || posType == "SBQ")
        {
            FIX::LongQty longQty;
            groupNumber.getField(longQty);
            number = longQty;
            cout << "====" << posType << "::" << longQty <<endl;
        }
        else if (posType == "SS" || posType == "SF")
        {
            FIX::ShortQty shortQty;
            groupNumber.getField(shortQty);

            cout << "====" << posType << "::" << shortQty <<endl;
        }
        Date date;
        if (posType == "SAV")
        {
            date = GetYesterday();
        }
        else if (posType == "SBQ")
        {
            date = GetToday();
        }
        else if (posType == "SF" || posType == "SS")
        {
            //被冻结了，可能是下单卖出但未成交
        }
        Time time = GetNow();
        Transaction transaction(stockCode, code, date, time, holdPrice, number);

        map<string, vector<Transaction> >::iterator iHolder = holder.stockHolder.find(code);
        if (iHolder != holder.stockHolder.end())
        {
            (iHolder->second).push_back(transaction);
        }
        else
        {
            vector<Transaction> vecTransaction;
            vecTransaction.push_back(transaction);
            holder.stockHolder[code] = vecTransaction;
        }
    }
}

void ExtractorUANMessage(const FIX::Message& message)
{
}
